package message;

import java.security.DigestException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class tail {
	String md5;

	public boolean checkMd5(String m) {
		MessageDigest md;
		try {
			md = MessageDigest.getInstance("md5");
			md.update(m.getBytes());

			byte[] result = new byte[16];
			md.digest(result, 0, 16);

			String md5 = tail.bytes2HexString(result).substring(0, 8);

			if (md5.equals(getMd5()))
				return true;
			else
				return false;
		} catch (Exception e) {
			e.printStackTrace();
			md = null;
			return false;
		}
	}

	@Override
	public String toString() {
		return getMd5();
	}
	public String getMd5() {
		return md5;
	}

	public void setMd5(String md5) {
		try {
			if (md5.length() != 8)
				throw new Exception("md5的长度应设置为8");
			else
				this.md5 = md5;
		} catch (Exception e) {
			System.out.println(e);
		}
	}

	public static String bytes2HexString(byte[] b) {
		String ret = "";
		for (int i = 0; i < b.length; i++) {
			String hex = Integer.toHexString(b[i] & 0xFF);
			if (hex.length() == 1) {
				hex = '0' + hex;
			}
			ret += hex.toUpperCase();
		}
		return ret;
	}
}
